//
//  UIColor+ABCDynamic.m
//  ABCAppearanceKit_Example
//
//  Created by mac on 2022/9/7.
//  Copyright © 2022 Joe. All rights reserved.
//

#import "UIColor+ABCDynamic.h"
#import <objc/runtime.h>

@implementation UIColor (ABCDynamic)

+ (UIColor *)abc_dynamicWithLightColor:(UIColor *)lightColor darkColor:(UIColor *)darkColor {
    if (@available(iOS 13.0, *)) {
       UIColor *dynamicColor = [UIColor colorWithDynamicProvider:^UIColor * _Nonnull(UITraitCollection * _Nonnull traitCollection) {
            if (traitCollection.userInterfaceStyle == UIUserInterfaceStyleDark) {
                return darkColor;
            }
            return lightColor;
        }];
        dynamicColor.lightColor = lightColor;
        dynamicColor.darkColor = darkColor;
        return dynamicColor;
    } else {
        return lightColor;
    }
}

- (void)setDarkColor:(UIColor *)darkColor {
    objc_setAssociatedObject(self, @selector(darkColor), darkColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)darkColor {
   return objc_getAssociatedObject(self, _cmd);
}

- (void)setLightColor:(UIColor *)lightColor {
    objc_setAssociatedObject(self, @selector(lightColor), lightColor, OBJC_ASSOCIATION_RETAIN_NONATOMIC);
}

- (UIColor *)lightColor {
    return objc_getAssociatedObject(self, _cmd);
}

@end
